﻿using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Loong.BackgroundJobs;
using Loong.Dapper.DbAccessors;
using Loong.Dapper.Uow;
using Loong.Dependency;
using Loong.Domain.Uow;
using Loong.Modules;

namespace Loong
{
    public sealed class SqlServerRepositoryModule : Module, ILoongModule
    {
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<ConnectionStringOptions>(configuration.GetSection(ConnectionStringOptions.SectionName));
        }

        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyByConvention(ThisAssembly);
            builder.RegisterDbAccessor(ThisAssembly);
            builder.RegisterType<SqlServerConnectionProvider>().As<IDbConnectionProvider>().InstancePerDependency();
            builder.RegisterType<DapperDbAccessorBase<BackgroundJobInfo, long>>().As<IDapperDbAccessor<BackgroundJobInfo, long>>().InstancePerLifetimeScope();
            builder.RegisterType<BackgroundJobStore>().As<IBackgroundJobStore>().InstancePerLifetimeScope();
        }

        public void Initialize()
        {
        }

        public void Shutdown()
        {
        }
    }
}
